/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-11-10 16:49:18
 * @LastEditTime: 2022-12-27 19:00:52
 */
import { createContext, useReducer, useEffect, useContext } from 'react';
import { SET_TABLE_DATA, SET_CHART_DATA } from '../constants';
import { normativeForStat, normativeForTable } from '@/api/statistics/statistics';

import { NormativeCensusReducer } from '../reducers/NormativeCensusReducer';
const initialState = {
  dataList: [],
  chartList: [],
  // 检测结果列表
  resultsStatusList: [
    { id: '0', name: '成功' },
    { id: '1', name: '失败' }
  ],
  // 检测方式
  detectionTypeList: [
    { id: 1, name: '条目与原文' },
    { id: 2, name: '条目' },
    { id: 3, name: '原文' },
    { id: 4, name: 'EEP格式包' },
    { id: 5, name: '光盘格式包' },
    { id: 6, name: '系统环境' }
  ]
};
const NormativeCensusContext = createContext();
const NormativeCensusContextProvider = (props) => {
  const [state, dispatch] = useReducer(NormativeCensusReducer, initialState);
  const actions = {
    setChartData(data) {
      normativeForStat({ ...data }).then((res) => {
        const { data } = res;
        dispatch({ type: SET_CHART_DATA, data });
      });
    },
    setTableData(data) {
      normativeForTable({ ...data }).then((res) => {
        const { data } = res;
        dispatch({ type: SET_TABLE_DATA, data });
      });
    }
  };
  const filters = {
    resultFilter(id) {
      return state.resultsStatusList.find((item) => item.id === id)?.name || '-';
    },
    detectionTypeFilter(id) {
      return state.detectionTypeList.find((item) => item.id === id)?.name || '-';
    }
  };
  useEffect(() => {}, []);
  return (
    <NormativeCensusContext.Provider value={{ ...state, ...actions, ...filters }}>
      {props.children}
    </NormativeCensusContext.Provider>
  );
};
export const useNormativeCensusContext = () => {
  return useContext(NormativeCensusContext);
};
export { NormativeCensusContext, NormativeCensusContextProvider };
